namespace SubhadraSolutions.Sharp.Logging.Context
{
    using SubhadraSolutions.Sharp.Logging.Contracts;
    using System.Web;

    public sealed class HttpGlobalContextStrategy : IGlobalContextStrategy
    {
        #region Indexers

        public object this[string name]
        {
            set
            {
                HttpContext currentContext = HttpContext.Current;
                if (currentContext != null)
                {
                    if (currentContext.Session != null)
                    {
                        if (value == null)
                        {
                            currentContext.Session.Remove(name);
                        }
                        else
                        {
                            currentContext.Session[name] = value;
                        }
                    }
                    if (value == null)
                    {
                        currentContext.Items.Remove(name);
                    }
                    else
                    {
                        currentContext.Items[name] = value;
                    }
                }
            }
            get
            {
                HttpContext currentContext = HttpContext.Current;
                if (currentContext != null)
                {
                    if (currentContext.Session != null)
                    {
                        object o = currentContext.Session[name];
                        if (o != null)
                        {
                            return o;
                        }
                    }
                    return currentContext.Items[name];
                }
                return null;
            }
        }

        #endregion Indexers

        #region Methods

        public void Remove(string name)
        {
            this[name] = null;
        }

        #endregion Methods
    }
}